﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ZombieMonkeys.Vibria
{
    public partial class NoteGrid : UserControl
    {
        Logic.Square square;

        public List<NoteSquareControl> noteGridControlList = new List<NoteSquareControl>();

        public NoteGrid(Logic.Square s)
        {
            square = s;
            InitializeComponent();
            PopulateGrid();
        }

        /// <summary>
        /// Method to set values for variables in each square
        /// </summary>
        /// <param name="puzzle"></param>
        private void PopulateGrid()
        {
            int i = 0;
            for (int r = 0; r < 2; r++)
            {
                for (int c = 0; c < 2; c++)
                {
                    NoteSquareControl noteSquareControl = new NoteSquareControl(r, c, i, square);
                    noteSquareControl.GetNoteGrid = this;
                    noteSquareControl.NoteValue = square.GetNoteValue(i);
                    noteSquareControl.SquareClicked += new EventHandler(ChildBoxClicked);
                    LayoutRoot.Children.Add(noteSquareControl);
                    Grid.SetRow(noteSquareControl, r);
                    Grid.SetColumn(noteSquareControl, c);
                    noteGridControlList.Add(noteSquareControl);
                    i++;
                }
            }
        }

        /// <summary>
        /// Method to update the value of the square
        /// </summary>
        /// <param name="sIn">string input</param>
        public void updateSquare(String sIn)
        {
            foreach (NoteSquareControl nsc in LayoutRoot.Children)
            {
                if (nsc.Selected)
                {
                    if (nsc.NoteValue != "")
                    {

                    }
                    else
                    {
                        nsc.NoteValue = sIn;
                    }
                }
            }
        }

        /// <summary>
        /// Event handler for clicks on squares within the control
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChildBoxClicked(object sender, EventArgs e)
        {
            foreach (NoteSquareControl nsc in LayoutRoot.Children)
            {
                nsc.Selected = false;
            }
        }
    }
}
